import {
  reqGetRankDataList,
  reqGetRankDetails,
  reqToLogin,
} from '@/api/rankList'
const state = {
  //排行榜内容
  rankdatalist: [],
  // 排行榜详情
  rankDetailData: {},
  //保存token
  savetoken: '',
  //校验账号密码
  checkRight: false,
}
const actions = {
  //获取排行榜内容
  async getRankDataList({ commit }) {
    const result = await reqGetRankDataList()

    if (result.code === 200) {
      commit('SAVE_RANK_DATA_LIST', result)
    }
  },
  //获取排行榜详情
  async getRankDetail({ commit }, id) {
    const result = await reqGetRankDetails(id)

    if (result.code === 200) {
      commit('SAVE_RANK_DETAIL', result.playlist)
    }
  },
  //保存token
  async toSaveToken({ commit }, [phNum, paNum]) {
    const result = await reqToLogin([phNum, paNum])
    if (result.code === 200) {
      commit('SAVE_TOKEN', result.token) && commit('CHECK_RIGHT', true)
    } else {
      commit('CHECK_RIGHT', false)
    }
  },
}
const mutations = {
  // 获取排行榜内容
  SAVE_RANK_DATA_LIST(state, payload) {
    state.rankdatalist = payload
  },
  //获取排行榜详情
  SAVE_RANK_DETAIL(state, payload) {
    state.rankDetailData = payload
  },
  //保存token
  SAVE_TOKEN(state, payload) {
    state.savetoken = payload
  },
  //校验账号密码
  CHECK_RIGHT(state, payload) {
    state.checkRight = payload
  },
}
const getters = {
  artistToplist(state) {
    return state.rankdatalist.artistToplist || {}
  },
  cloudList(state) {
    return (
      (state.rankdatalist.list && state.rankdatalist.list.slice(0, 4)) || []
    )
  },
  earthList(state) {
    return (state.rankdatalist.list && state.rankdatalist.list.slice(4)) || []
  },
  rewardToplist(state) {
    return state.rankdatalist.rewardToplist || {}
  },
}
export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters,
}
